﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using TrainingManagement.api.Models.BusinessLogic;
using TrainingManagement.api.Models.BusinessLogic.SkillSet;
using TrainingManagement.api.Models.BusinessObjects;
using Provausio.Common.Utility.Extensions;
using TrainingManagement.api.Models.BusinessObjects.Responses;

namespace TrainingManagement.api.Controllers
{
    public class SkillSetController : ApiController
    {
        public SkillSet PutSkillSet(SkillSet skillSet)
        {
            return new SkillSetCreateOperation().Run(skillSet);
        }

        [System.Web.Http.ActionName("Update")]
        public void PostUpdateSkillSet(SkillSet skillSet)
        {
            new SkillSetUpdateOperation().Run(skillSet);
        }

        [System.Web.Http.ActionName("ItemsInsert")]
        public SkillSet PostUpdateSkillSetItems(SkillSet skillSet)
        {
            if(skillSet.SkillSetItems.Any(a => a.RetrainFreq == null))
                throw new Exception("All skills must specify a retrain frequency when adding to a skill set");

            return new SkillSetUpdateItemsOperation().Run(skillSet);
        }

        public void DeleteSkillSet(string skillSetId)
        {
            var id = skillSetId.ToType<int>();
            new SkillSetDeleteOperation().Run(id);
        }

        public List<SkillSet> GetSkillSet(int? skillSetid)
        {
            return new SkillSetSelectOperation().Run(skillSetid);
        } 
    }
}
